<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapperPackage}.${prefix}.command.${modelName}CommandMapper">
    <insert id="save" parameterType="${modelPackage}.${prefix}.${modelName}" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ${tableName}(
        <#assign sysFields=["create_time","update_time","delete_flag","id"]>
        <#list columnInfoList as col>
            <#if !sysFields?seq_contains(col.name)>
            `${col.name}`,
            </#if>
        </#list>
            create_time,
            update_time,
            delete_flag
        )
        VALUES (
        <#list columnInfoList as col>
            <#if !sysFields?seq_contains(col.name)>
            <#if col.name == 'company_id'>
            ${r'#'}{company.id},
            <#else>
            ${r'#'}{${col.modelColumnName}},
            </#if>
            </#if>
        </#list>
            NOW(),
            NOW(),
            0
        )
    </insert>

    <insert id="bulkSave" useGeneratedKeys="true" keyProperty="id"
            parameterType="${modelPackage}.${prefix}.${modelName}">
        INSERT INTO ${tableName}(
        <#assign sysFields=["create_time","update_time","id","delete_flag"]>
        <#list columnInfoList as col>
            <#if !sysFields?seq_contains(col.name)>
                ${col.name},
            </#if>
        </#list>
            delete_flag,
            create_time,
            update_time
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            <#list columnInfoList as col>
                <#if !sysFields?seq_contains(col.name)>
                    <#if col.name == 'company_id'>
                        ${r'#'}{item.company.id},
                    <#else>
                        ${r'#'}{item.${col.modelColumnName}},
                    </#if>
                </#if>
            </#list>
            0,
            NOW(),
            NOW()
            )
        </foreach>
    </insert>

    <update id="update">
        UPDATE ${tableName}
        <set>
        <#assign sysUpdateFields=["update_user_id","update_time","delete_flag","company_id","id","create_time","create_user_id"]>
        <#list columnInfoList as col>
            <#if !sysUpdateFields?seq_contains(col.name)>
            <if test="${col.modelColumnName} != null">
                `${col.name}` = ${r'#'}{${col.modelColumnName}},
            </if>
            </#if>
        </#list>
            update_user_id=${r'#'}{updateUserId},
            update_time=NOW()
        </set>
        WHERE id = ${r'#{id}'}
    </update>

    <update id="bulkUpdate">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            UPDATE ${tableName} SET
            <#assign sysUpdateFields=["update_time","company_id","id","create_time", "create_user_id"]>
            <#list columnInfoList as col>
                <#if !sysUpdateFields?seq_contains(col.name)>
                    <if test="item.${col.modelColumnName} != null">
                        ${col.name} = ${r'#'}{item.${col.modelColumnName}},
                    </if>
                </#if>
            </#list>
            update_time=NOW()
            WHERE id = ${r'#{item.id}'}
        </foreach>
    </update>

    <delete id="deleteById">
        UPDATE ${tableName} SET delete_flag = 1,update_time=NOW() WHERE id = ${r'#{id}'}
    </delete>

    <delete id="bulkDeleteByIds">
        UPDATE ${tableName}
        <set>
            delete_flag = 1,
            update_time=NOW()
        </set>
        WHERE id IN
        <foreach collection="collection" open="(" close=")" index="index" item="item" separator=",">
            ${r'#'}{item}
        </foreach>
    </delete>
</mapper>
